﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ImageTools;

namespace Kiva7.Controls
{
    public class BaseImageControl : UserControl, IImageSourceCache
    {
        protected BaseImageControl()
        {
            _ImageSourceCache = new ImageSourceCache(this);

        }

        protected virtual Image Image
        {
            get
            {
                return null;
            }
        }

        private ImageSourceCache _ImageSourceCache;

        protected void EntityChanged()
        {
            _ImageSourceCache.EntityChanged();
        }

        protected void ImageOpened(object sender, RoutedEventArgs e)
        {
            _ImageSourceCache.ImageOpened(sender, e);

        }


        #region "Abstract"

        public virtual bool ApplyFilter(ref ExtendedImage extendedImage)
        {
            IImageFilter filter = GetFilter();
            if (filter != null)
            {
                ExtendedImage temp = extendedImage;

                temp = extendedImage.Clone();

                filter.Apply(temp, extendedImage, temp.Bounds);

                extendedImage = temp;
                return true;
            }

            return false;
        }

        protected virtual IImageFilter GetFilter()
        {
            //return new ImageTools.Filtering.Sepia();
            return null;
        }

        public virtual Uri CreateImageUri()
        {
            return null;
        }

        public virtual string GetImageDirectoryName()
        {
            return null;
        }

        public virtual  string GetImagePath()
        {
            return null;
        }


        public ImageSource Source
        {
            get
            {
                //Deployment.Current.Dispatcher.BeginInvoke(()=>
    
                return Image.Source;
            }
            set
            {
                Image.Source = value;
            }
        }

        public void SetOpacity()
        {
            Image.Opacity = 1;
        }

        #endregion

    }
}
